/**
 *
 */
package com.evil.DB_Utils;

import com.evil.DB_Utils.QueryCallBack.Next;

import java.sql.*;

/******************************************
 * 工程名：Download<br>
 * 包名：com.evil.DB_Utils<br>
 * 时间：2016年6月10日 上午10:38:29<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：查询<br>
 ******************************************/
public class Query extends DB_Utils
{
    private ResultSet resultSet;
    private PreparedStatement preparedStatement;
    private String url;

    private int index;

    /**
     * 构造函数
     *
     * @param url
     */
    public Query(String url)
    {
        this.url = url;
        if (url.equals(""))
            System.err.println("url地址不能为空");
        else
        {
            index = 1;
            try
            {
                preparedStatement = (PreparedStatement) connection
                        .prepareStatement(url);
            }
            catch (SQLException e)
            {
                if (e.toString().indexOf("java.lang.NullPointerException") > 0)
                    System.out.println("数据库可能未连接");
                else
                    e.printStackTrace();
            }
        }
    }

    /**
     * 添加
     *
     * @param s
     * @return
     */
    public Query add(String s)
    {
        try
        {
            preparedStatement.setString(index, s);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param i
     * @return
     */
    public Query add(int i)
    {
        try
        {
            preparedStatement.setInt(index, i);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param date
     * @return
     */
    public Query add(Date date)
    {
        try
        {
            preparedStatement.setDate(index, date);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param f
     * @return
     */
    public Query add(float f)
    {
        try
        {
            preparedStatement.setFloat(index, f);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 添加
     *
     * @param object
     * @return
     */
    public Query add(Object object)
    {
        try
        {
            preparedStatement.setObject(index, object);
            index++;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 建立链接，必须调用
     *
     * @return
     */
    public Query build()
    {
        try
        {
            resultSet = preparedStatement.executeQuery();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    public boolean isNext()
    {
        try
        {
            if (resultSet.next())
                return true;
            else
                return false;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获得resultSet，包含while
     *
     * @param callBack
     * @return
     */
    public Query getAll(QueryCallBack callBack)
    {
        Next next = new Next();
        try
        {
            while (resultSet.next())
            {
                callBack.onCallBack(next.set(resultSet));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        next.close();
        return this;
    }

    /**
     * 获得resultSet，不包含while
     *
     * @param callBack
     * @return
     */
    public Query get(QueryCallBack callBack)
    {
        Next next = new Next();
        callBack.onCallBack(next.set(resultSet));
        next.close();
        return this;
    }

    /*********************************************************************************************
     * 单个值查询区域
     */

    /**
     * resultSet.next
     * <p>
     * 无用值，仅仅区别与上面的next
     *
     * @return
     */
    public Query next()
    {
        try
        {
            resultSet.next();
            return this;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查找变量
     */

    public String getString(int index)
    {
        try
        {
            return resultSet.getString(index);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public int getInt(int index)
    {
        try
        {
            return resultSet.getInt(index);
        }
        catch (SQLException e)
        {
            //e.printStackTrace();
        }
        return 0;
    }

    public Date getDate(int index)
    {
        try
        {
            return resultSet.getDate(index);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public Time getTime(int index)
    {
        try
        {
            return resultSet.getTime(index);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public Object getObject(int index)
    {
        try
        {
            return resultSet.getObject(index);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭流
     */

    public void close()
    {
        try
        {
            if (resultSet != null)
                resultSet.close();
            if (preparedStatement != null)
                preparedStatement.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

}
